using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNetNuke;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace Jfc007.WebLib
{
    public partial class Settings : ModuleSettingsBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sa = string.Empty;
        }
        public override void LoadSettings()
        {
            try
            {
                if (Page.IsPostBack == false)
                {

                    BindSearchResults();
                    DotNetNuke.Entities.Modules.ModuleController objModules = new ModuleController();

                    string searchTabID = Convert.ToString(ModuleSettings["TabDisplayBinaryData"]);


                    if (!String.IsNullOrEmpty(searchTabID))
                    {
                        if ((cboModule.Items.FindByValue(searchTabID) != null))
                        {
                            cboModule.Items.FindByValue(searchTabID).Selected = true;
                        }
                    }
                    else
                    {
                        if (cboModule.Items.Count > 0)
                        {
                            objModules.UpdateModuleSetting(this.ModuleId, "TabDisplayBinaryData", cboModule.SelectedItem.Value);
                        }
                    }

                    if (cboModule.Items.Count > 0)
                    {
                        txtModule.Text = cboModule.SelectedItem.Text;
                    }
                    else
                    {
                        txtModule.Text = Localization.GetString("NoSearchModule", LocalResourceFile);
                    }

                    Pop3ServerController pop3Controller = new Pop3ServerController();
                    Pop3ServerInfo pop3Server = pop3Controller.GetPop3Server(PortalId);
                    if (pop3Server != null)
                    {

                        txtPassword.Text = pop3Server.Password;
                        txtPort.Text = pop3Server.Port;
                        txtServerName.Text = pop3Server.ServerName;
                        txtUserName.Text = pop3Server.User;
                        ckbUseSSL.Checked = pop3Server.UseSsl;
                    }
                    Session.Add("Server", pop3Server);
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        public override void UpdateSettings()
        {
            try
            {
                DotNetNuke.Entities.Modules.ModuleController objModules = new ModuleController();


                if (!(cboModule.SelectedIndex == -1))
                {
                    objModules.UpdateModuleSetting(this.ModuleId, "TabDisplayBinaryData", cboModule.SelectedItem.Value);
                }

                Pop3ServerInfo pop3server = new Pop3ServerInfo();
                pop3server.PortalId = PortalId;
                pop3server.Password = txtPassword.Text;
                pop3server.Port = txtPort.Text;
                pop3server.ServerName = txtServerName.Text;
                pop3server.User = txtUserName.Text;
                pop3server.UseSsl = ckbUseSSL.Checked;

                Pop3ServerController pop3Controller = new Pop3ServerController();

                if (((Pop3ServerInfo)Session["Server"]) == null)
                {
                    pop3Controller.AddPop3Server(pop3server);
                }
                else
                {
                    pop3Controller.UpdatePop3Server(pop3server);
                }


            }
            catch (Exception exc) //Module failed to load
            {

                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
        #region Private Methods

        /// <summary>
        /// BindSearchResults gets the Search Results Modules available and binds them to the drop-down combo
        /// </summary>
        private void BindSearchResults()
        {

            //cboModule.DataSource = CrmAnnouncementController.GetViewCrmAnnouncementModules( PortalId );
            DocumentsController controller = new DocumentsController();

            cboModule.DataSource = controller.GetSearchTabDisplayBinaryData(PortalId);
            cboModule.DataTextField = "SearchTabName";
            cboModule.DataValueField = "SearchTabId";
            cboModule.DataBind();
            if (cboModule.Items.Count < 2)
            {
                cboModule.Visible = false;
                txtModule.Visible = true;
            }
            else
            {
                cboModule.Visible = true;
                txtModule.Visible = false;
            }
        }
        #endregion
    }
}